package com.zjsru.plan2024.oneday;

/**
 * 999. 可以被一步捕获的棋子数
 * @Author: cookLee
 * @Date: 2024-12-06
 */
public class NumRookCaptures {

    /**
     * 主
     * \
     * 给定一个 8 x 8 的棋盘，只有一个 白色的车，用字符 'R' 表示。棋盘上还可能存在白色的象 'B' 以及黑色的卒 'p'。空方块用字符 '.' 表示。
     * \
     * 输入：[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
     * 输出：3
     * 解释：
     * 在本例中，车能够吃掉所有的卒。
     * \
     *  输入：[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
     * 输出：0
     * 解释：
     * 象阻止了车吃掉任何卒。
     * \
     * @param args args
     */
    public static void main(String[] args) {
        NumRookCaptures numRookCaptures = new NumRookCaptures();
        char[][] chars = new char[][]{
                {'.', '.', '.', '.', '.', '.', '.', '.'},
                {'.', '.', '.', 'p', '.', '.', '.', '.'},
                {'.', '.', '.', 'R', '.', '.', '.', 'p'},
                {'.', '.', '.', '.', '.', '.', '.', '.'},
                {'.', '.', '.', '.', '.', '.', '.', '.'},
                {'.', '.', '.', 'p', '.', '.', '.', '.'},
                {'.', '.', '.', '.', '.', '.', '.', '.'},
                {'.', '.', '.', '.', '.', '.', '.', '.'}

        };
        System.out.println(numRookCaptures.numRookCaptures(chars));
    }

    private final int[][] POSITION = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};

    /**
     * 模拟
     *
     * @param board 板
     * @return int
     */
    public int numRookCaptures(char[][] board) {
        //寻找唯一的车
        int len = board.length;
        int x = 0;
        int y = 0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                }
            }
        }
        return this.helperOne(x, y, board, len);
//        return this.helperTwo(x, y, board, len);
    }

    /**
     * 计算范围方法一
     *
     * @param x     x
     * @param y     y
     * @param board 板
     * @param len   len
     * @return int
     */
    private int helperOne(int x, int y, char[][] board, int len) {
        int ans = 0;
        //通过车向外扩展，遇到B即停止
        for (int[] POS : this.POSITION) {
            int newX = x + POS[0];
            int newY = y + POS[1];
            //循环判断是否越界，是"."添加步长,表示可走的位置
            while (newX >= 0 && newX < len && newY >= 0 && newY < len && board[newX][newY] == '.') {
                newX += POS[0];
                newY += POS[1];
            }
            //判断是否越界，表示可吃掉的棋子
            if (newX >= 0 && newX < len && newY >= 0 && newY < len && board[newX][newY] == 'p') {
                ans++;
            }
        }
        return ans;
    }

    /**
     * 计算范围方法一
     *
     * @param x     x
     * @param y     y
     * @param board 板
     * @param len   len
     * @return int
     */
    public int helperTwo(int x, int y, char[][] board, int len) {
        int ans = 0;
        //通过车向外扩展，遇到B即停止
        for (int[] POS : this.POSITION) {
            int newX = x + POS[0];
            int newY = y + POS[1];
            //循环判断是否越界，判断B和p的属性做操作
            while (newX >= 0 && newX < len && newY >= 0 && newY < len) {
                if (board[newX][newY] == 'B') {
                    break;
                }
                //范围内可吃，不包含棋子后面的棋子
                if (board[newX][newY] == 'p') {
                    ans++;
                    break;
                }
                newX += POS[0];
                newY += POS[1];
            }
        }
        return ans;
    }


}
